using System.Collections.Generic;
using System.Threading.Tasks;
using Ant.DbServiceInterface;
using Ant.Dto.StoreDto;
using Ant.EntityFrameworkCore.Model;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Ant.DbService
{
    public class StoreDataService : IStoreDataService
    {
        private AntDB2Context _dbContext;
        public StoreDataService(AntDB2Context dbContext)
        {
            _dbContext = dbContext;
        }

        public bool AddCommodity(CommodityInfo com)
        {
            _dbContext.CommodityInfos.Add(com);
            return _dbContext.SaveChanges() > 0;
        }

        public bool AddCommodityType(CommodityType comType)
        {
            _dbContext.CommodityTypes.Add(comType);
            return _dbContext.SaveChanges() > 0;
        }

        public int AddStore(StoreInfo addStore)
        {
            _dbContext.StoreInfos.Add(addStore);
            return _dbContext.SaveChanges() > 0?addStore.StoreId:-1;
        }

        public async Task<ICollection<StoreInfo>> GetAllStore()
        {
            return await _dbContext.StoreInfos.ToListAsync();
        }

        public async Task<ICollection<CommodityInfo>> GetCommodity(int storeID, int pageSize, int current, string key)
        {
            double temp = 0;
            var query = _dbContext.CommodityInfos.Where(c => c.StoreId == storeID);
            if (key != null)
            {
                if (double.TryParse(key, out temp))
                {
                    query = query.Where(c => c.BarCode.Contains(key));
                }
                else
                {
                    query = query.Where(c => c.Cname.Contains(key));
                }
            }
            // query = query.Skip((current-1)*pageSize).Take(pageSize);
            return await query.ToListAsync();
        }

        public async Task<ICollection<CommodityInfo>> GetCommodity(int storeID)
        {
            return await _dbContext.CommodityInfos.Where(c=>c.StoreId==storeID).ToListAsync();
        }

        public async Task<ICollection<CommodityType>> GetCommodityType(int storeID)
        {
            return await _dbContext.CommodityTypes.Where(c => c.StoreId == storeID).ToListAsync();
        }

        public Task<ICollection<CommodityInfo>> GetHotCommidit(int storeID)
        {
            throw new System.NotImplementedException();
        }

        public async Task<ICollection<CommodityInfo>> SearchCommodityForStore(int storeID, string key)
        {
            double intKey = 0;
            var a = double.TryParse(key, out intKey);

            if (a)
            {
                return await _dbContext.CommodityInfos.Where(c => c.StoreId == storeID && c.BarCode.Contains(key)).ToListAsync();
            }
            else
            {
                return await _dbContext.CommodityInfos.Where(c => c.StoreId == storeID && c.Cname.Contains(key)).ToListAsync();
            }

        }



        public bool UpdateCommodity(int storeID, UpdataCommodityDto dto)
        {
            var com = _dbContext.CommodityInfos.Where(c => c.Cid == dto.Cid && c.StoreId == storeID).FirstOrDefault();
            if (com == null) return false;
            com.Cname = dto.Cname == null ? com.Cname : dto.Cname;
            com.BarCode = dto.BarCode == null ? com.BarCode : dto.BarCode;
            com.Price = dto.Price == 0 ? com.Price : dto.Price;
            com.Specification = dto.Specification == null ? com.Specification : dto.Specification;
            com.Unit = dto.Unit == null ? com.Unit : dto.Unit;
            return _dbContext.SaveChanges() > 0;
        }
        
        
    }
}